/*
 * [ string ]
 * [ string ]   变为  [ string ] [ string ]
 */

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <stdexcept>
#include "ZiFuScreenZhanWeiJiSuan.h"
using namespace std;

class Fix{
public:
    Fix(string&& a, string&& b, size_t Ln, size_t Col) : _ln(Ln), _col(Col)
    {
        _main = std::forward<string&&>(a);
        _add = std::forward<string&&>(b);

        if (_main.empty() || _add.empty()) throw std::length_error("没有提供字符串 Conflict!");


        if (_main.back() != '\n') _main += '\n';
        if (_add.back() != '\n') _add += '\n';
    }
    string StartFix() {
        std::vector<std::string> split_main;
        std::vector<std::string> split_add;
        std::string result;

        size_t start = 0;
        while (start < _main.size()) {
            size_t end = _main.find('\n', start);
            if (end == std::string_view::npos) {
                end = _main.size();
                split_main.emplace_back(_main.substr(start, end - start));
                break;
            }
            split_main.emplace_back(_main.substr(start, end - start + 1));
            start = end + 1; // 跳过换行符
        }

        start = 0;
        while (start < _add.size()) {
            size_t end = _add.find('\n', start);
            if (end == std::string_view::npos) {
                end = _add.size();
                split_add.emplace_back(_add.substr(start, end - start));
                break;
            }
            split_add.emplace_back(_add.substr(start, end - start + 1));
            start = end + 1; // 跳过换行符
        }

        int maintext_col_max_len{0};
        for (string& item : split_main)
        {
            maintext_col_max_len = maintext_col_max_len > static_cast<int>(ZiFuScreenZhanWeiJiSuan::count_display_width(item)) ? maintext_col_max_len : static_cast<int>(ZiFuScreenZhanWeiJiSuan::count_display_width(item));
        }
        _col = maintext_col_max_len + static_cast<int>(_col);

        if (_col < 4)
            _col = 4;
        else
            _col = (_col | 3) + 1;

        int add_spaceing{};
        size_t j = 0;
        for (size_t i = 0; i < split_main.size(); i++) {
            result.append(split_main[i]);

            if ((i + 1 < _ln) || (i + 1 > _ln + split_add.size() - 1)) {
                continue;
            }

            add_spaceing = static_cast<int>(_col) - static_cast<int>(ZiFuScreenZhanWeiJiSuan::count_display_width(split_main[i]));

            if(add_spaceing <= 0){
                cout <<add_spaceing<<endl;
                cout <<_col<<endl;
                cout <<static_cast<int>(ZiFuScreenZhanWeiJiSuan::count_display_width(split_main[i]))<<endl;
                cout <<i<<endl;
                throw std::range_error("The number of columns given is too small. Conflict!");
            }
            result.pop_back();
            result.insert(result.end(), add_spaceing + 1, ' ');
            result.append(split_add[j]);
            ++j;
        }
        if (j < split_add.size()) {
            for (; j < split_add.size(); ++j) {
                result.insert(result.end(), _col, ' ');
                result.append(split_add[j]);
            }
        }
        return result;
    }
private:
    string _main;
    string _add;
    size_t _ln;
    size_t _col;
};